package trading_strategy;

import trading_strategy.events.EventDispatcher;
import trading_strategy.events.EventManager;
import trading_strategy.events.history.TRTHInstrumentReader;
import trading_strategy.instruments.Future;
import trading_strategy.markets.Market;
import trading_strategy.markets.ZeroTransactionCosts;
import trading_strategy.orders.Brokers;
import trading_strategy.orders.InstrumentOrderBroker;
import trading_strategy.orders.simulation.SimulatedExchange;
import trading_strategy.strategies.SuccessiveTicksStrategy;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * Created by IntelliJ IDEA.
 * User: gderoujoux
 * Date: 6 mai 2010
 * Time: 09:15:56
 * To change this template use File | Settings | File Templates.
 */
public class TestSuccessiveTick {
    public static void main(String[] args) throws ParseException {
        final Market eurex = new Market(1, "EUR", new ZeroTransactionCosts());
        DateFormat sdf = new SimpleDateFormat("yyyyMMdd-HHmm");
        Future future = new Future(302790, "CAC_FUT", eurex.getId(), sdf.parse("20100618-1100"), 10, 0.5);

        final TRTHInstrumentReader hr = new TRTHInstrumentReader(future, "history/20100112/" + future.getUnderlyingId() + ".Csv");

        SimulatedExchange exchange = SimulatedExchange.getExchange(future); // Instantiates the simulated exchange
        Brokers.getInstance().registerBroker(future, new InstrumentOrderBroker(exchange)); // The broker

        final SuccessiveTicksStrategy strategy = new SuccessiveTicksStrategy(future);
        strategy.register();

        // TODO start the market event reader and wait...
        hr.read();

        System.out.println("Read");
        EventManager.getInstance().getDispatcher(EventManager.REPLAY_DISPATCHER).addEvent(new EventDispatcher.TimedRunnable(Long.MAX_VALUE) {
            @Override
            public void run() {
                System.out.println("PnL: " + strategy.getPnL());
                System.exit(0);
            }
        });
    }
}
